/**
 *author:yhb
 */
package com.towker.frame.dao.rbac;

import java.util.*;

import org.hibernate.dialect.Ingres10Dialect;
import org.springframework.stereotype.Repository;

import com.towker.core.db.*;
import com.towker.frame.model.rbac.Resource;

@Repository
public class ResourceDao extends BaseDao<Resource, Long>
{
	@SuppressWarnings("unchecked")
	@Override
	public Class getEntityClass(){return Resource.class;}
	/**
	 * 删除系统下的所有资源*/
	public void delResourceBySystemid(Long systemid){
		this.getSqlSessionTemplate().delete(this.getIbatisMapperNamespace()+".delResourceBySystemid", systemid);
	}
	/** 删除系统资源*/
	public void delResource(Long id){
		this.getSqlSessionTemplate().delete(this.getIbatisMapperNamespace()+".delResource", id);
	}
	/**给资源排序*/
	public void sortResource(Long id,Long sequence){
		Map<String,Long> map=new HashMap<String,Long>();
		map.put("id",id);map.put("sequence",sequence);
		getSqlSessionTemplate().update(getIbatisMapperNamespace()+".sortResource", map);
	}
	/**获得系统下面的所有资源*/
	@SuppressWarnings("unchecked")
	public List<Resource> getResBySysid(long sysid){
		return getSqlSessionTemplate().selectList(getIbatisMapperNamespace() + ".getResBySysid", sysid);
	}
	/**根据编号、系统编号获得父类资源下面的所有子资源*/
	@SuppressWarnings("unchecked")
	public List<Resource> getChildrenResByIdAndSysid(long id,long sysid){
		Map<String, Long> map=new HashMap<String, Long>();
		map.put("id", id);map.put("sysid", sysid);
		return (List<Resource>)this.getSqlSessionTemplate().selectList(getIbatisMapperNamespace() + ".getChildrenResByIdAndSysid", map);
	}
	/**根据系统与用户获得资源*/
	@SuppressWarnings("unchecked")
	public List<Resource> getUserResouce(long sys, long user){
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("system", sys);map.put("user", user);
		return (List<Resource>) this.getSqlSessionTemplate().selectList(getIbatisMapperNamespace() + ".getUserResouce", map);
	}
	/**根据系统编号，获得该系统中的所有资源的资源列表*/
	@SuppressWarnings("unchecked")
	public List<Resource> getAllResBySysId(long sysid){
		return (List<Resource>)this.getSqlSessionTemplate().selectList(getIbatisMapperNamespace() + ".getAllResBySysId", sysid);
	}
	/**根据角色编号，获得该角色的所有已经分配的资源*/
	@SuppressWarnings("unchecked")
	public List<Resource> getAssignedResOfRole(long sysid){
		return (List<Resource>)this.getSqlSessionTemplate().selectList(getIbatisMapperNamespace() + ".getAssignedResOfRole", sysid);
	}
	@SuppressWarnings("unchecked")
	public List<Resource> getChildrenResById(Long id){
		return (List<Resource>)getSqlSessionTemplate().selectList(getIbatisMapperNamespace()+".getChildrenResById", id);
	}
	/**根据角色编号获得该角色的所有资源*/
	@SuppressWarnings("unchecked")
	public List<Resource> getResByRoleid(long roleid){
		return (List<Resource>)getSqlSessionTemplate().selectList(getIbatisMapperNamespace() + ".getResByRoleid", roleid);
	}
	/**根据用户编号与用户进入的系统编号，获得所有可用资源列表*/
	@SuppressWarnings("unchecked")
	public List<Resource> getAvailableResByUseridAndSysid(long userid,long sysid){
		Map<String, Long> map=new HashMap<String, Long>();
		map.put("userid",userid);map.put("sysid", sysid);
		return getSqlSessionTemplate().selectList(getIbatisMapperNamespace() + ".getAvailableResByUseridAndSysid", map);
	}
	@SuppressWarnings("unchecked")
	public List<Resource> getAvailableResByUseridAndSysid2(long userid,long sysid){
		Map<String, Long> map=new HashMap<String, Long>();
		map.put("userid",userid);map.put("sysid", sysid);
		return getSqlSessionTemplate().selectList(getIbatisMapperNamespace() + ".getAvailableResByUseridAndSysid2", map);
	}
	/** 获得 */
	@SuppressWarnings("unchecked")
	public List<Resource> getAssignedResByUseridAndSysid(long userid,long sysid){
		Map<String, Long> map=new HashMap<String, Long>();
		map.put("userid",userid);map.put("sysid", sysid);
		return getSqlSessionTemplate().selectList(getIbatisMapperNamespace() + ".getAssignedResByUseridAndSysid", map);
	}
	@SuppressWarnings("unchecked")
	public List<Resource> getRootResBySysid(Long sysid){
		return (List<Resource>)getSqlSessionTemplate().selectList(getIbatisMapperNamespace()+".getRootResBySysid",sysid);
	}
	/**获得用户权限*/
	@SuppressWarnings("unchecked")
	public List<Resource> getUserPermission(Long userid,Long sysid){
		Map<String, Long> map=new HashMap<String, Long>();
		map.put("userid",userid);map.put("sysid", sysid);
		return (List<Resource>)getSqlSessionTemplate().selectList(getIbatisMapperNamespace()+".getUserPermission", map);
	}
	/**
	 * 获得用户资源树*/
	@SuppressWarnings("unchecked")
	public List<Resource> getUserRes(Long userid,Long sysid, int resType){
		Map map=new HashMap<String,Long>();
		map.put("userid",userid);map.put("sysid", sysid);
		if (resType !=0) {
			map.put("resType", resType + "");
		} else {
			map.put("resType", "2,1");
		}
		System.out.println("resType: " + map.get("resType"));
		return (List<Resource>)getSqlSessionTemplate().selectList(getIbatisMapperNamespace()+".getUserRes", map);
	}
}
